package com.googlecode.chelonia;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;



/**
 * utility class to work with stes of countries, only africa at the moment.
 * @author creichlin
 *
 */

public class CountrySet implements Cloneable, Iterable<String>{
    
    public static CountrySet SET_AFRICA = null;
    public static CountrySet SET_EAST_AFRICA = null;
    public static CountrySet SET_CENTRAL_AFRICA = null;
    public static CountrySet SET_NORTHERN_AFRICA = null;
    public static CountrySet SET_WEST_AFRICA = null;
    public static CountrySet SET_SOUTHERN_AFRICA = null;
    
    public final static String ALGERIA = "dz";    // northern
    public final static String ANGOLA = "ao";    // southern
    public final static String BENIN = "bj";    // west
    public final static String BOTSWANA = "bw";    //  southern
    public final static String BOUVET_ISLAND = "bv";    // 
    public final static String BURKINA_FASO = "bf";    // west
    public final static String BURUNDI = "bi";    // east
    public final static String CAMEROON = "cm";    // central
    public final static String CAPE_VERDE = "cv";    // west
    public final static String CENTRAL_AFRICAN_REPUBLIC = "cf";    // central
    public final static String CHAD = "td";    // west
    public final static String COMOROS = "km";    // east
    public final static String REPUBLIC_OF_THE_CONGO = "cg";    // central
    public final static String DEMOCRATIC_REPUBLIC_OF_THE_CONGO = "cd";    // central
    public final static String DJIBOUTI = "dj";    // east
    public final static String EGYPT = "eg";    // northern
    public final static String EQUATORIAL_GUINEA = "gq";    // central
    public final static String ERITREA = "er";    // east
    public final static String ETHIOPIA = "et";    // east
    public final static String GABON = "ga";    // central
    public final static String GAMBIA = "gm";    // west
    public final static String GHANA = "gh";    // west
    public final static String GUINEA = "gn";    // west
    public final static String GUINEA_BISSAU = "gw";    // west
    public final static String IVORY_COAST = "ci";    // west
    public final static String KENYA = "ke";    // east
    public final static String LESOTHO = "ls";    //  southern
    public final static String LIBERIA = "lr";    // west
    public final static String LIBYA = "ly";    // northern
    public final static String MADAGASCAR = "mg";    //  southern
    public final static String MALAWI = "mv";    //  southern
    public final static String MALI = "ml";    // west
    public final static String MAURITANIA = "mr";    // west
    public final static String MAURITIUS = "mu";    //  southern
    public final static String MAYOTTE = "yt";    // 
    public final static String MOROCCO = "ma";    // northern
    public final static String MOZAMBIQUE = "mz";    //  southern
    public final static String NAMIBIA = "na";    //  southern
    public final static String NIGER = "ne";    // west
    public final static String NIGERIA = "ng";    // west
    public final static String REUNION = "re";    //  southern
    public final static String RWANDA = "rw";    // east
    public final static String SAO_TOME_AND_PRINCIPE = "st";    // central
    public final static String SENEGAL = "sn";    // west
    public final static String SEYCHELLES = "sc";    // east
    public final static String SIERRA_LEONE = "sl";    // west
    public final static String SOMALIA = "so";    // east
    public final static String SOUTH_AFRICA = "za";    //  southern
    public final static String SUDAN = "sd";    // northern
    public final static String SWAZILAND = "sz";    //  southern
    public final static String TANZANIA = "tz";    // east
    public final static String TOGO = "tg";    // west
    public final static String TUNISIA = "tn";    // northern
    public final static String UGANDA = "ug";    // east
    public final static String ZAMBIA = "zm";    //  southern
    public final static String ZIMBABWE = "zw";    //  southern
    public final static String WEST_SAHARA = "eh"; // west
        
    static{
        SET_AFRICA = new CountrySet();
        SET_NORTHERN_AFRICA = new CountrySet();
        SET_WEST_AFRICA = new CountrySet();
        SET_EAST_AFRICA = new CountrySet();
        SET_CENTRAL_AFRICA = new CountrySet();
        SET_SOUTHERN_AFRICA = new CountrySet();
        
        SET_EAST_AFRICA.add(BURUNDI);
        SET_EAST_AFRICA.add(COMOROS);
        SET_EAST_AFRICA.add(DJIBOUTI);
        SET_EAST_AFRICA.add(ERITREA);
        SET_EAST_AFRICA.add(ETHIOPIA);
        SET_EAST_AFRICA.add(KENYA);
        SET_EAST_AFRICA.add(RWANDA);
        SET_EAST_AFRICA.add(SEYCHELLES);
        SET_EAST_AFRICA.add(SOMALIA);
        SET_EAST_AFRICA.add(TANZANIA);
        SET_EAST_AFRICA.add(UGANDA);
        
        SET_CENTRAL_AFRICA.add(CAMEROON);
        SET_CENTRAL_AFRICA.add(CENTRAL_AFRICAN_REPUBLIC);
        SET_CENTRAL_AFRICA.add(DEMOCRATIC_REPUBLIC_OF_THE_CONGO);
        SET_CENTRAL_AFRICA.add(REPUBLIC_OF_THE_CONGO);
        SET_CENTRAL_AFRICA.add(EQUATORIAL_GUINEA);
        SET_CENTRAL_AFRICA.add(GABON);
        SET_CENTRAL_AFRICA.add(SAO_TOME_AND_PRINCIPE);
        
        SET_NORTHERN_AFRICA.add(ALGERIA);
        SET_NORTHERN_AFRICA.add(EGYPT);
        SET_NORTHERN_AFRICA.add(LIBYA);
        SET_NORTHERN_AFRICA.add(MOROCCO);
        SET_NORTHERN_AFRICA.add(SUDAN);
        SET_NORTHERN_AFRICA.add(TUNISIA);

        SET_SOUTHERN_AFRICA.add(ANGOLA);
        SET_SOUTHERN_AFRICA.add(BOTSWANA);
        SET_SOUTHERN_AFRICA.add(LESOTHO);
        SET_SOUTHERN_AFRICA.add(MADAGASCAR);
        SET_SOUTHERN_AFRICA.add(MALAWI);
        SET_SOUTHERN_AFRICA.add(MAURITIUS);
        SET_SOUTHERN_AFRICA.add(MOZAMBIQUE);
        SET_SOUTHERN_AFRICA.add(NAMIBIA);
        SET_SOUTHERN_AFRICA.add(REUNION);
        SET_SOUTHERN_AFRICA.add(SOUTH_AFRICA);
        SET_SOUTHERN_AFRICA.add(SWAZILAND);
        SET_SOUTHERN_AFRICA.add(ZAMBIA);
        SET_SOUTHERN_AFRICA.add(ZIMBABWE);
        
        SET_WEST_AFRICA.add(BENIN);
        SET_WEST_AFRICA.add(BURKINA_FASO);
        SET_WEST_AFRICA.add(CAPE_VERDE);
        SET_WEST_AFRICA.add(CHAD);
        SET_WEST_AFRICA.add(IVORY_COAST);
        SET_WEST_AFRICA.add(GAMBIA);
        SET_WEST_AFRICA.add(GHANA);
        SET_WEST_AFRICA.add(GUINEA);
        SET_WEST_AFRICA.add(GUINEA_BISSAU);
        SET_WEST_AFRICA.add(LIBERIA);
        SET_WEST_AFRICA.add(MALI);
        SET_WEST_AFRICA.add(MAURITANIA);
        SET_WEST_AFRICA.add(NIGER);
        SET_WEST_AFRICA.add(NIGERIA);
        SET_WEST_AFRICA.add(SENEGAL);
        SET_WEST_AFRICA.add(SIERRA_LEONE);
        SET_WEST_AFRICA.add(TOGO);
        SET_WEST_AFRICA.add(WEST_SAHARA);
        
        SET_AFRICA.add(SET_CENTRAL_AFRICA);
        SET_AFRICA.add(SET_EAST_AFRICA);
        SET_AFRICA.add(SET_NORTHERN_AFRICA);
        SET_AFRICA.add(SET_SOUTHERN_AFRICA);
        SET_AFRICA.add(SET_WEST_AFRICA);
        
    }
    
    private Set<String> data = new HashSet<String>();
    
    public CountrySet(){
    	
    }
    
    public CountrySet(String in){
    	for(String tk:in.split(",")){
    		add(tk);
    	}
    }
    
    public void add(String country){
    	if("africa".equals(country)){
    		add(SET_AFRICA);
    	}else if("east_africa".equals(country)){
    		add(SET_EAST_AFRICA);
    	}else if("northern_africa".equals(country)){
    		add(SET_NORTHERN_AFRICA);
    	}else if("west_africa".equals(country)){
    		add(SET_WEST_AFRICA);
    	}else if("southern_africa".equals(country)){
    		add(SET_SOUTHERN_AFRICA);
    	}else if("central_africa".equals(country)){
    		add(SET_CENTRAL_AFRICA);
    	}else{
    		data.add(country);
    	}
    }
    
    public void add(CountrySet country){
    	data.addAll(country.data);
    }

    public void remove(String country){
        data.remove(country);
    }
    
    public void remove(CountrySet country){
    	data.removeAll(country.data);
    }
    
    public boolean contains(String cc){
        return data.contains(cc);
    }
    
    public boolean contains(CountrySet cs){
    	return data.containsAll(cs.data);
    }
    
    public Set<String> getCountryCodes(){
        return Collections.unmodifiableSet(data);
    }
    
    @Override
	public String toString(){
        StringBuilder sb = new StringBuilder();
        
        for(String s:data){
        	sb.append(s + ",");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

	public Iterator<String> iterator() {
		return data.iterator();
	}
}
